<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 21:09
 */

namespace app\wx\controller;


use app\tools\Curl;
use app\tools\MIniprogram;
use app\tools\Wx;

class App
{
    function __construct()
    {
        error_reporting(0);
    }

    function index(){

        //        验证 token

        $nonce     = $_GET['nonce'];
        $token     = 'token123876';
        $timestamp = $_GET['timestamp'];
        $echostr   = $_GET['echostr'];
        $signature = $_GET['signature'];
        //形成数组，然后按字典序排序
        $array = array();
        $array = array($nonce, $timestamp, $token);
        sort($array);
        //拼接成字符串,sha1加密 ，然后与signature进行校验
        $str = sha1( implode( $array ) );
        if( $str == $signature && $echostr ){
            //第一次接入weixin api接口的时候
            echo  $echostr;
            exit;
        }

        $data = file_get_contents("php://input");
//        file_put_contents("1.txt", $data);

        $ob = json_decode($data, true);

        if ($ob['MsgType'] == "text"){

            $data  = db('kf')->field('number, path')->select();
            foreach ($data as $v)
            {
                if($ob['Content'] == $v['number'])
                {
                    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".MIniprogram::accessToken();
                    $data = [
                        'touser'=> $ob['FromUserName'],
                        'msgtype'=> 'image',
                        'image'=> [
                            'media_id'=> MIniprogram::uploadImage($v['path']),
                        ],
                    ];
                    curl($url, json_encode($data));

                    exit('success');
                }
            }
        }

    }



}